unit Buble;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;
const
 n=70;
 x0=5;
 y0=5;
 H=10;
 w=16;

type

  TForm1 = class(TForm)
    Timer1: TTimer;
    Image1: TImage;
    Button1: TButton;
    ScrollBar1: TScrollBar;
    procedure Timer1Timer(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ScrollBar1Change(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
 arr: array[0..n-1] of integer;
  i, j, runnumber, flag, l, a, v: integer;




  Form1: TForm1;

implementation



{$R *.dfm}
   procedure draw;
begin
 form1.image1.canvas.Rectangle(0, 0, form1.image1.width, form1.image1.height);
    for i:= 0 to n-1 do
     if(i =l ) or (I= l +1 ) then
       begin
        form1.Image1.canvas.brush.Color:=clRed;
           form1.image1.canvas.rectangle(x0 + w*i, y0, x0+w*(i+1), y0+h*arr[i]);
       end else begin
       Form1.Image1.Canvas.Brush.Color:=clWhite;
         form1.image1.canvas.rectangle(x0 + w*i, y0, x0+w*(i+1), y0+h*arr[i]);

       end;
      end;


procedure TForm1.Timer1Timer(Sender: TObject);
begin
if(runnumber <> (n-2)*(n-2)+2*n-2) then


  begin
If (l = n-1) then
 begin
 L:=0;
  Form1.Image1.Canvas.Brush.Color:=clWhite;
       //If (flag  = 0 )  then
   //begin
   //Timer1.Enabled:=False;
      //Application.MessageBox('Here you are!', 'Yeah!');
      //end else begin
       //flag:= 0;
       //end;
 end else begin



 if (arr[l+1]>arr[l]) then
  begin
   //flag:=flag+1;
   v:=arr[l];
   arr[l]:=arr[l+1];
    arr[l+1]:=v;
   end;

  Draw;
  Form1.Refresh;
  runnumber:=runnumber+1;
  l:=l+1;
  v:=0;
  //flag:=0;
   end;

end else begin


    Timer1.Enabled:=False;
     Application.MessageBox('Here you are!', 'Yeah!');
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
runnumber:=0;
 Timer1.Enabled:=True;
 randomize;
 for i:=0 to n-1 do
  begin
  repeat
   arr[i]:=random(70);
  until arr[i]<>0;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
l:=0;

    Timer1.Enabled:=False;



end;

procedure TForm1.ScrollBar1Change(Sender: TObject);
begin
timer1.interval:=scrollbar1.position;
end;

end.
